package com.starling.extend.core
{
	import com.starling.extend.components.BTextInput;
	import com.starling.extend.display.BButton;
	import com.starling.extend.display.BDisplay;
	import com.starling.extend.display.BImage;
	import com.starling.extend.display.BMovieClip;
	import com.starling.extend.display.BScale9Image;
	import com.starling.extend.display.BSprite;
	import com.starling.extend.display.BTextField;
	
	import feathers.controls.Button;
	import feathers.controls.List;
	import feathers.controls.NumericStepper;
	import feathers.controls.PageIndicator;
	import feathers.controls.PickerList;
	import feathers.controls.ProgressBar;
	import feathers.controls.ScrollText;
	import feathers.controls.Slider;
	import feathers.controls.TabBar;
	import feathers.controls.popups.VerticalCenteredPopUpContentManager;
	import feathers.data.ListCollection;
	import feathers.display.Scale9Image;
	import feathers.layout.AnchorLayoutData;
	import feathers.text.BitmapFontTextFormat;
	import feathers.textures.Scale9Textures;
	
	import flash.display.BlendMode;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	import starling.core.Starling;
	import starling.display.BlendMode;
	import starling.display.DisplayObject;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.filters.BlurFilter;
	import starling.filters.ColorMatrixFilter;
	import starling.text.BitmapFont;
	import starling.text.TextField;
	import starling.text.TextFieldAutoSize;
	import starling.textures.Texture;
	import starling.utils.AssetManager;
	import starling.utils.HAlign;
	import starling.utils.VAlign;
	
	public class PacketDecoder
	{
		/** core 位图 */
		public static const TYPE_IMAGE:String = "img";
		/** core 影片剪辑 */
		public static const TYPE_MOVIECLIP:String = "mc";
		/** core 九宫格 */
		public static const TYPE_SCALE9GRID:String = "s9";
		/** core Sprite */
		public static const TYPE_SPRITE:String = "spr";
		/** core 文本 */
		public static const TYPE_TEXT:String = "txt";	
		/** core 按钮 */
		public static const TYPE_BUTTON:String = "btn";
		
		
		/** complete 拖动文本 */
		public static const COMP_SCROLLTEXT:String = "comp_scrolltext";
		/** complete 开关 */
		public static const COMP_TOGGLESWITCH:String = "comp_toggleswitch";
		/** complete 多选 */
		public static const COMP_CHECK:String = "comp_check";
		/** complete 单选 */
		public static const COMP_RADIO:String = "comp_radio";
		/** complete 标签切换 */
		public static const COMP_TABBAR:String = "comp_tabbar";
		/** complete 拖动条 */
		public static const COMP_SLIDER:String = "comp_slider";
		/** complete 进度条 */
		public static const COMP_PROGRESSBAR:String = "comp_progressbar";
		/** complete 选择菜单 */
		public static const COMP_PICKERLIST:String = "comp_pickerlist";
		/** complete 输入文本 */
		public static const COMP_PAGEINDICATOR:String = "comp_pageindicator";
		/** complete 数值步进 */
		public static const COMP_NUMERICSTEPPER:String = "comp_numericstepper";
		/** complete 列表 */
		public static const COMP_LIST:String = "comp_list";
		
		
		
		public static const ANGLE_TO_RADIAN:Number = Math.PI / 180;
		
		private var _bytesData:Object;
		private var _assets:AssetManager;
		private static const _bitmapFonts:Vector.<BitmapFont> = new Vector.<BitmapFont>();
		
		public function PacketDecoder(bytesName:String ,assets:AssetManager)
		{
			var newBytes:ByteArray = BObject.clone(assets.getByteArray(bytesName));
			newBytes.uncompress();
			
			_bytesData = JSON.parse(new String(newBytes));
			_assets = assets;
			
			newBytes.clear();
			newBytes = null;
			
			BSystem.gc();
		}
		
		
		/**
		 * 添加位图字体 
		 * @param bitmapFont
		 * 
		 */		
		public static function addBitmapFont(bitmapFont:BitmapFont):void
		{
			_bitmapFonts.push(bitmapFont);
		}
		
		
		
		/**
		 * 回收 
		 * 
		 */		
		public function dispose():void
		{
			if(_bytesData!=null)
				_bytesData = null;
			BSystem.gc();
		}
		
		
		
		
		
		/**
		 * 创建图片 
		 * @param name
		 * @return 
		 * 
		 */		
		public function createImage(name:String):BImage
		{
			var data:Object = _bytesData[name];
			var image:BImage = new BImage(_assets.getTexture(name));
			image.pivotX = data.pivotX;
			image.pivotY = data.pivotY;
			image.textureName = name;
			return image;
		}
		
		
		/**
		 * 创建9宫 
		 * @param name
		 * @param info
		 * @return 
		 * 
		 */			
		public function createScale9Image(name:String ,info:Object=null):BScale9Image
		{
			var data:Object = _bytesData[name];
			var s9Texture:Scale9Textures = new Scale9Textures(
				_assets.getTexture(name),
				new Rectangle(data.x ,data.y ,data.width ,data.height)
			);
			
			var s9image:BScale9Image = new BScale9Image(s9Texture);
			if(info!=null)
			{
				s9image.width = info.width;
				s9image.height = info.height;
			}
			s9image.textureName = name;
			
			return s9image;
		}
		
		
		/**
		 * 创建动态文本 
		 * @param info
		 * @return 
		 * 
		 */		
		public function createTextField(info:Object=null):BTextField
		{
			var btf:BTextField = new BTextField(1,1,"");
			if(info!=null)btf.style = info;
			return btf;
		}
		
		/**
		 * 创建输入文本 
		 * @param info
		 * @return 
		 * 
		 */		
		public function createTextInput(info:Object=null):BTextInput
		{
			var bti:BTextInput = new BTextInput();
			if(info!=null)bti.style = info;
			return bti;
		}
		
		
		/**
		 * 创建按钮 
		 * @param name
		 * @return 
		 * 
		 */		
		public function createButton(name:String):BButton
		{
			return null;
		}
		
		
		
		
		/**
		 * 创建Sprite 
		 * @param spriteName
		 * @return 
		 * 
		 */		
		public function createSprite(name:String):BSprite
		{
			var sprite:BSprite = new BSprite();
			sprite.textureName = name;
			
			var data:Array = _bytesData[name];
			
			for (var i:int=0;i<data.length;i++)
			{
				var display:DisplayObject;
				switch(data[i].textureType)
				{
					case TYPE_IMAGE:		
						display = createImage(data[i].textureName);							
						break;
					
					case TYPE_SPRITE:		
						display = createSprite(data[i].textureName);						
						break;
					
					case TYPE_MOVIECLIP:	
						display = createMovieClip(data[i].textureName);						
						break;
					
					case TYPE_BUTTON:
						display = createButton(data[i].textureName);
						break;
					
					case TYPE_SCALE9GRID:	
						display = createScale9Image(data[i].textureName ,data[i]);			
						break;
					
					case TYPE_TEXT:	
						display = data[i].textInfo.type == "dynamic"?createTextField(data[i].textInfo):createTextInput(data[i].textInfo);	
						break;
					
					default:
						display = createComponent(data[i]);
						break;
				}
				
				BDisplay.setInfo(display ,data[i]);
				sprite.addChild(display);
			}
			
			return sprite;
		}
		
		
		
		
		/**
		 * 创建影片剪辑 
		 * @param name
		 * @return 
		 * 
		 */		
		public function createMovieClip(name:String):BMovieClip
		{
			var data:Object = _bytesData[name];
			var displayObject:Object = new Object();
			var childs:Array = data.childs;
			
			for (var i:int=0;i<childs.length;i++)
			{
				switch(childs[i].textureType)
				{
					case TYPE_IMAGE:
						displayObject[childs[i].textureName] = createImage(childs[i].textureName);
						break;
					
					case TYPE_SPRITE:
						displayObject[childs[i].textureName] = createSprite(childs[i].textureName);
						break;
					
					case TYPE_MOVIECLIP:
						displayObject[childs[i].textureName] = createMovieClip(childs[i].textureName);
						break;
					
					case TYPE_BUTTON:
						displayObject[childs[i].textureName] = createButton(childs[i].textureName);
						break;
					
					case TYPE_SCALE9GRID:
						displayObject[childs[i].textureName] = createScale9Image(childs[i].textureName);
						break;
					
					case TYPE_TEXT:	
						displayObject[childs[i].textureName] = childs[i].type=="dynamic"?createTextField():createTextInput();
						break;
					
					default:
						displayObject[childs[i].textureName] = createComponent(data[i]);
						break;
				}
			}
			
			var bmc:BMovieClip = new BMovieClip(displayObject ,data.infos);
			bmc.textureName = name;
			return bmc;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		/**
		 * 创建组件 
		 * @param data
		 * @return 
		 * 
		 */		
		private function createComponent(data:Object):DisplayObject
		{
			switch(data.textureType)
			{
				case COMP_SCROLLTEXT:
					return ComponentDecoder.createScrollText(data.compInfo ,this);
					
				case COMP_TOGGLESWITCH:		
					return ComponentDecoder.createToggleSwitch(data.compInfo ,this);
				
				case COMP_CHECK:
					return ComponentDecoder.createCheck(data.compInfo ,this);
					
				case COMP_RADIO:
					return ComponentDecoder.createRadio(data.compInfo ,this);
					
				case COMP_TABBAR:
					return ComponentDecoder.createTabbar(data.compInfo ,this);
					
				case COMP_SLIDER:	
					return ComponentDecoder.createSlider(data.compInfo ,this);;
					
				case COMP_PROGRESSBAR:	
					return ComponentDecoder.createProgressBar(data.compInfo ,this);;
					
				case COMP_PAGEINDICATOR:
					return ComponentDecoder.createPageIndicator(data.compInfo ,this);;
					
				case COMP_PICKERLIST:	
					var pl:PickerList = ComponentDecoder.createPickerList(
						createScale9Image("s9_button_up"),
						createScale9Image("s9_button_disabled"),
						createScale9Image("s9_button_up") ,
						createScale9Image("s9_button_down")
					);
					
					var popUpContentManager:VerticalCenteredPopUpContentManager = new VerticalCenteredPopUpContentManager();
					popUpContentManager.marginTop = 20;
					popUpContentManager.marginRight = 25;
					popUpContentManager.marginBottom = 20;
					popUpContentManager.marginLeft = 25;
					pl.popUpContentManager = popUpContentManager;
					
					var groceryList:ListCollection = new ListCollection(
						[
							{ text: "Milk", thumbnail: _assets.getTexture("img_background_disabled") },
							{ text: "Eggs", thumbnail: _assets.getTexture("img_background_disabled") },
							{ text: "Bread", thumbnail: _assets.getTexture("img_background_disabled") },
							{ text: "Chicken", thumbnail: _assets.getTexture("img_background_disabled") },
						]);
					pl.dataProvider = groceryList;
					return pl;
					
				/*case COMP_NUMERICSTEPPER:	
					return;*/
					
				/*case COMP_LIST:		
					return;*/
			}
			
			return null;
		}
	}
}